package net.tngou.iask.action.api;

import java.sql.Date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.enums.LevelEnum;
import net.tngou.iask.pojo.Access;
import net.tngou.iask.pojo.Cure;
import net.tngou.iask.pojo.Disease;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.pojo.Patient;
import net.tngou.iask.pojo.Survey;
import net.tngou.iask.service.AccessService;
import net.tngou.iask.service.CureService;
import net.tngou.iask.service.DiseaseService;
import net.tngou.iask.service.PatientService;
import net.tngou.iask.util.DateUtil;
import net.tngou.iask.util.IDUtil;
import net.tngou.iask.util.IDUtil.No;
import net.tngou.iask.util.StringUtil;

/**
 * 治疗操作
 * @author tngou@tngou.net
 *
 */
public class CureApi extends BaseAction {

	/**
	 * 添加治疗
	 */
	public void add() {
		if(user==null){
			run_nologin();return;
		}
		if((user.getRole()!=3&&user.getRole()!=4&&user.getRole()!=5&&user.getRole()!=6)||user.getMain()==0)
		{
			run_false("没有添加权限");return;
		}
		Cure bean = getAsk(Cure.class);
		if(StringUtil.isNotEmpty(bean.getNumber())){
			POJO pojo = bean.get(new HashMap<String,Object>(){{
				put("number", bean.getNumber());
			}});
			if(pojo!=null){
				run_false("该治疗号已经存在！");return;
			}
		}
		Patient patient;
		if(bean.getPatient()>0)
		{
			patient = new Patient().get(bean.getPatient());
			if(patient==null||bean.getDisease()==0)
			{
				run_false("请求的参数不正确！");return;
			}
			
		}else
		{
			patient = getAsk(Patient.class);
			if(isEmpty(patient.getNo(),patient.getName())||bean.getDisease()==0)
			{
				run_false("请求的参数不正确！");return;	
			}
			No no = IDUtil.GetNo(patient.getNo());
			if(no==null)
			{
				run_false("身份证号码不正确！");return;
			}
			@SuppressWarnings("serial")
			Patient pojo = new Patient().get(new HashMap<String,Object>(){{
				put("no", patient.getNo());
			}});
			PatientService service = new PatientService();
			if(pojo!=null)
			{
				bean.setPatient(pojo.getId());
				String phone=request.getParameter("phone");
				if(isEmpty(phone))
				{
					service.saveLinkmain(pojo, phone, 0, 1);
				}
				
			}else
			{
				
				String phone=request.getParameter("phone");
				if(isEmpty(phone))
				{
					run_false("请求的参数不正确！");return;	
				}
				long id = service.save(patient, no, user,phone,0);
				if(id>0)
				{
					bean.setPatient(id);					
				}else
				{
					run_false("信息错误！");return;
				}
			}
		}
		
		Disease disease = new Disease().get(bean.getDisease());
		if(disease==null)
		{
			run_false("信息错误！");return;
		}
		String[] place=request.getParameterValues("places");//取得发病位置
		String[] users=request.getParameterValues("users");//主管医生
		String[] treatment=request.getParameterValues("treatments");//取得治疗方式
		String[] department=request.getParameterValues("departments");//取得相关科室
		CureService service = new CureService();
		bean.setMain(user.getMain());
		bean.setUser(user.getId());
		
		long id = service.save(bean, disease, place, treatment, department, users);
		if(id>0){
			saveLog(LevelEnum.system, "添加治疗(id:"+id+"-"+patient.getName()+")");
			run_success(id+"");
		}
		else run_false("添加失败！");
		
		
	}
	
	
	/**
	 * 更新治疗
	 */
	public void update() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=3&&user.getRole()!=4&&user.getRole()!=5&&user.getRole()!=6)
		{
			run_false("没有编辑权限");return;
		}
		Cure bean = getAsk(Cure.class);
		if(bean.getId()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Cure cure = new Cure().get(bean.getId());
		if(cure==null||cure.getMain()!=user.getMain())
		{
			run_false("请求的参数不正确！");return;
		}
		if(StringUtil.isNotEmpty(bean.getDescription()))cure.setDescription(bean.getDescription());
		if(bean.getTdate()!=null)cure.setTdate(bean.getTdate());
		if(bean.getStartdate()!=null)cure.setStartdate(bean.getStartdate());
		if(bean.getEnddate()!=null)cure.setEnddate(bean.getEnddate());
		if(bean.getCycle()!=0)cure.setCycle(bean.getCycle());
		if(StringUtil.isNotEmpty(request.getParameter("type")))cure.setType(bean.getType());
		long id = cure.update();
		if(id>0){
			String[] place=request.getParameterValues("places");//取得发病位置
			String[] users=request.getParameterValues("users");//主管医生
			String[] treatment=request.getParameterValues("treatments");//取得治疗方式
			String[] department=request.getParameterValues("departments");//取得相关科室
			CureService service = new CureService();
			service.update(id, place, treatment, department, users);
			saveLog(LevelEnum.system, "添加更新(id:"+id+")");
			run_success("更新成功");
		}
		else run_false("更新失败！");
		
		
		
	}
	
	/**
	 * 更新治疗中的疾病
	 * @return
	 */
	public void diseaseupdate() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=3&&user.getRole()!=4&&user.getRole()!=5&&user.getRole()!=6)
		{
			run_false("没有编辑权限");return;
		}
		Cure bean = getAsk(Cure.class);
		if(bean.getId()==0||bean.getDisease()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Disease disease = new Disease().get(bean.getDisease());
		Cure cure = new Cure().get(bean.getId());
		if(disease==null||cure==null||cure.getMain()!=user.getMain())
		{
			run_false("请求的参数不正确！");return;
		}
		cure.setDisease(bean.getDisease());
		long id = cure.update();
		if(id>0)
		{
			saveLog(LevelEnum.common, "更新治疗疾病(id:"+id+"-"+")");
			run_success("更新成功！");return;
		}else
		{
			run_false("更新失败！");return;
		}
	}
	
	/**
	 * 更新随访问卷
	 */
	public void surveyupdate() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=3&&user.getRole()!=4&&user.getRole()!=5&&user.getRole()!=6)
		{
			run_false("没有编辑权限");return;
		}
		Cure bean = getAsk(Cure.class);
		if(bean.getId()==0||bean.getSurvey()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Survey survey = new Survey().get(bean.getSurvey());
		Cure cure = new Cure().get(bean.getId());
		if(survey==null||cure==null||cure.getMain()!=user.getMain())
		{
			run_false("请求的参数不正确！");return;
		}
		cure.setSurvey(bean.getSurvey());
		long id = cure.update();
		if(id>0)
		{
			saveLog(LevelEnum.common, "更新治疗执行问卷(id:"+id+"-"+")");
			run_success("更新成功！");return;
		}else
		{
			run_false("更新失败！");return;
		}
	}
	
	/**
	 * 添加随访
	 */
	public void accessadd() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=3&&user.getRole()!=4&&user.getRole()!=5&&user.getRole()!=6)
		{
			run_false("没有编辑权限");return;
		}
		Cure bean = getAsk(Cure.class);
		if(bean.getId()==0||bean.getSurvey()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Survey survey = new Survey().get(bean.getSurvey());
		Cure cure = new Cure().get(bean.getId());
		if(survey==null||cure==null||cure.getMain()!=user.getMain())
		{
			run_false("请求的参数不正确！");return;
		}	
		CureService service = new CureService();
		long id = service.saveAccess(cure, survey.getId());
		if(id>0)
		{
			LocalDateTime ltime = LocalDateTime.now();
			cure.setLfdate(DateUtil.toSqlDate(ltime));//最近随访时间
			LocalDateTime ftime = ltime.plusDays(cure.getCycle());
			cure.setNfdate(DateUtil.toSqlDate(ftime));
			cure.update();
			saveLog(LevelEnum.common, "更新执行访问问卷(id:"+id+"-"+")");
			run_success("添加成功！");return;
		}else
		{
			run_false("添加失败！");return;
		}
				
	}
}
